/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using VMukti.DataAccess;
using System.Data.SqlClient;
using System.Collections.Generic;
using VMukti.Business.CommonDataContracts;
using VMuktiAPI;


namespace VMukti.Business
{
    public class ClsModuleCollection : ClsBaseCollection<ClsModuleLogic>
    {
        public static ClsModuleCollection GetAll()
        {
            ClsModuleCollection obj = new ClsModuleCollection();
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {
                try
                {
                obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    e.Data.Add("My Key", "GetAll()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.CommunicationException e)
                {
                    e.Data.Add("My Key", "GetAll()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module order by ModuleName").dsInfo);
                }
            }
            else
            {
                obj.MapObjects(new ClsModuleDataService().GetModules());
            }
            return obj;
        }

        public static ClsModuleCollection GetNCMod()
        {
            ClsModuleCollection obj = new ClsModuleCollection();
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {
                try
                {
                obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName, id, IsCollaborative, ZipFile, AssemblyFile from Module where NeedsAuthentication='true'and IsCollaborative='false' order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    e.Data.Add("My Key", "GetNCMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='true'and IsCollaborative='false' order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.CommunicationException e)
                {
                    e.Data.Add("My Key", "GetNCMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='true'and IsCollaborative='false' order by ModuleName").dsInfo);
                }
            }
            else
            {
                obj.MapObjects(new ClsModuleDataService().GetNonCollaborativeModules());
            }
            return obj;
        }

        public static ClsModuleCollection GetNonAuthenticatedMod()
        {
            ClsModuleCollection obj = new ClsModuleCollection();
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {
                try
                {
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' and IsDeleted='false' order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    e.Data.Add("My Key", "GetNonAuthenticatedMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.CommunicationException e)
                {
                    e.Data.Add("My Key", "GetNonAuthenticatedMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName").dsInfo);
                }
                
                //List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                //clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                //objInfo.Direction = "Input";
                //objInfo.PName = "@pRoleId";
                //objInfo.PValue = 1;                
                //objInfo.PDBType = "BigInt";
                //objInfo.PSize = 200;
                //lstSP.Add(objInfo);

                ////clsSqlParametersInfo objInfo1 = new clsSqlParametersInfo();
                ////objInfo1.Direction = "Output";
                ////objInfo1.PName = "@Result";
                ////objInfo1.PDBType = "Bit";
                ////objInfo1.PSize = 1;
                ////lstSP.Add(objInfo1);

                ////SqlParameter sp = new SqlParameter();
                
                ////sp.Direction = System.Data.ParameterDirection.Input;
                ////sp.ParameterName = "@pModuleName";
                
                ////sp.Value="Chat";
                ////sp.SqlDbType = System.Data.SqlDbType.NVarChar;
                ////lstSP.Add(sp);

                ////SqlParameter sp1 = new SqlParameter();
                ////sp1.Direction = System.Data.ParameterDirection.Output;
                ////sp1.ParameterName = "@Result";
                ////sp1.SqlDbType = System.Data.SqlDbType.Bit;
                ////lstSP.Add(sp1);

                //clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                //CSqlInfo.objParam = lstSP;

                //VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo);
                //VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where NeedsAuthentication='false' order by ModuleName");
            }
            else
            {
                obj.MapObjects(new ClsModuleDataService().GetNonAuthenticatedModules());
            }
            return obj;
        }

        public static ClsModuleCollection GetCMod(int intRoleId)
        {
            ClsModuleCollection obj = new ClsModuleCollection();
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {                
                List<clsSqlParametersInfo> lstSP = new List<clsSqlParametersInfo>();

                clsSqlParametersInfo objInfo = new clsSqlParametersInfo();
                objInfo.Direction = "Input";
                objInfo.PName = "@pRoleId";
                objInfo.PValue = intRoleId;
                objInfo.PDBType = "BigInt";
                objInfo.PSize = 200;
                lstSP.Add(objInfo);

                clsSqlParameterContract CSqlInfo = new clsSqlParameterContract();
                CSqlInfo.objParam = lstSP;

                try
                {
                obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo).dsInfo);
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    e.Data.Add("My Key", "GetCMod(int intRoleId)--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo).dsInfo);
                }
                catch (System.ServiceModel.CommunicationException e)
                {
                    e.Data.Add("My Key", "GetCMod(int intRoleId)--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("spGCModules", CSqlInfo).dsInfo);
                }

            }
            else
            {
                obj.MapObjects(new ClsModuleDataService().GetCollaborativeModules(intRoleId));
            }
            return obj;
        }
        public static ClsModuleCollection GetOnlyCollMod()
        {
            ClsModuleCollection obj = new ClsModuleCollection();
            if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
            {                
                try
                {
                obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where IsCollaborative='true' and NeedsAuthentication='true' order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    e.Data.Add("My Key", "GetOnlyCollMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where IsCollaborative='true' and NeedsAuthentication='true' order by ModuleName").dsInfo);
                }
                catch (System.ServiceModel.CommunicationException e)
                {
                    e.Data.Add("My Key", "GetOnlyCollMod()--:--ClsModuleCollection.cs--:--" + e.Message + " :--:--");
                    ClsException.LogError(e);
                    ClsException.WriteToErrorLogFile(e);
                    VMuktiAPI.VMuktiHelper.CallEvent("GetSuperNodeIP", null, null);
                    obj.MapObjects(VMukti.Business.clsDataBaseChannel.chHttpDataBaseService.svcExecuteDataSet("select ModuleName,id,IsCollaborative,ZipFile,AssemblyFile from Module where IsCollaborative='true' and NeedsAuthentication='true' order by ModuleName").dsInfo);
                }
            }
            else
            {
                obj.MapObjects(new ClsModuleDataService().GetOnlyCollaborativeModules());
            }
            return obj;
        }
    }
}
